<?php
namespace core\http;
use Psr\Http\Message\UriInterface;

class Request extends HttpMessage implements \Psr\Http\Message\RequestInterface
{

    protected $method;
    /**
     * @var string
     */
    private $uri;

    private $param=[];
    private $get=[];
    private $post=[];
    private $file=[];

    public function __construct($server)
    {
        $this->server = $server;
        $this->buildParam();
    }

    public function buildParam()
    {
        $this->get = $_GET;
        $this->post = $_POST;
        $this->file = $_FILES;
        //接收JSON代码
        if (strtolower($this->server['CONTENT_TYPE']) == 'application/json' && $json = file_get_contents("php://input") ) {
            $this->post = array_merge($this->post,@json_decode($json, true));
        }
        //接收XML
        if (strtolower($this->server['CONTENT_TYPE']) == 'application/xml' && $content = file_get_contents("php://input") ) {
            $xml = simplexml_load_string($content);//转换post数据为simplexml对象
            $this->post = array_merge($this->post,xmlToArray($xml));
        }
    }

    // 创建一个请求
    public static function create($server)
    {
        return new static($server); // new 自己
    }

    //获取全部参数
    public function request()
    {

    }

    public function withParam($key,$val='')
    {
        if(is_array($key)){
            $this->param = array_merge($this->param,$key);
        }
        if(is_string($key)){
            $this->param[$key] = $val;
        }
    }
    public function param()
    {
        return $this->param;
    }
    public function get(){

    }

    public function post(){

    }

    public function file(){

    }

    public function getRequestTarget()
    {
        // TODO: Implement getRequestTarget() method.
    }

    public function withRequestTarget($requestTarget)
    {
        // TODO: Implement withRequestTarget() method.
    }

    public function getMethod()
    {
        if (null !== $this->method) {
            return $this->method;
        }
        return $this->method = strtoupper($this->server['REQUEST_METHOD']??'');
    }

    public function withMethod($method)
    {
        // TODO: Implement withMethod() method.
    }

    public function getUri()
    {
        if (null !== $this->uri) {
            return $this->uri;
        }
        return $this->uri = $this->server['REQUEST_URI']??'';
    }

    public function withUri(UriInterface $uri, $preserveHost = false)
    {
        // TODO: Implement withUri() method.
    }
}